<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>用户管理</title>
	<link rel="stylesheet" href="../EasyUI-1.5.3/themes/default/easyui.css">
    <link rel="stylesheet" href="../css/icon.css">
    <link rel="stylesheet" href="../css/index.css">
    <script src="../js/jquery-1.8.0.min.js"></script>
    <script src="../EasyUI-1.5.3/jquery.min.js"></script>
    <script src="../EasyUI-1.5.3/jquery.easyui.min.js" ></script>
    <script src="../EasyUI-1.5.3/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="../js/jquery.serializejson.min.js"></script>
    <script src="../js/xheditor/xheditor-1.2.2.min.js" charset="utf-8"></script>
	<script src="../js/xheditor/xheditor_lang/zh-cn.js" charset="utf-8"></script>
</head>
<body>
	 <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'center',border:false">
            <!-- Begin of toolbar -->
            <div id="wu-toolbar">
                <div class="wu-toolbar-button">
                    <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="openAdd()" plain="true">添加</a>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="commentsClear()" plain="true">删除</a>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-save" onclick="replace()" plain="true">返回</a>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="reload()" plain="true">刷新</a>
                </div>
            </div>
            <!-- End of toolbar -->
            <!-- <table id="content-data" toolbar="#wu-toolbar"></table> -->
           <table id="dg" class="easyui-datagrid" style="width:100%;height:100%"></table>
        </div>
    </div>
    <script type="text/javascript">
        // $('#dg').datagrid({
        //     url:'http://localhost:3000/comments/list',
        //     method:'post',
        //     striped:true,   //设置表单渐变色
        //     // nowrap:false,    //换行显示
        //     rownumbers:true,
        //     singleSelect:true,
        //     pageSize:10,
        //     pageList:[10,20,30,40],
        //     pagination:true,
        //     multiSort:true,
        //     fitColumns:true,
        //     // onClickCell: onClickCell,
        //     columns:[[
        //         { field:'null',checkbox:true},
        //         { field:'newsTitle',title:'评论文章标题',width:$(this).width()*0.04,sortable:true,editor:'text'},
        //         { field:'newsAuthor',title:'作者',width:$(this).width()*0.03,sortable:true,editor:'text'},
        //         { field:'classifyID',title:'类别',width:$(this).width()*0.04,sortable:true},
        //         { field:'date',title:'日期',width:$(this).width()*0.04,sortable:true},
        //         { field:'content',title:'评论内容',width:$(this).width()*0.1,sortable:true,editor:'text'},
        //         { field:'username',title:'评论人',width:$(this).width()*0.04,sortable:true},
        //         { field:'operate',title:'评论管理',align:'center',width:$(this).width()*0.08,
        //             formatter:function(value, row, index){
        //                 var str = '<a href="javascript:;" name="clear" class="easyui-linkbutton" onClick="commentsClear()" >';
        //                 return str;
        //             }}
        //     ]],
        //     onLoadSuccess:function(data){
        //        $("a[name='clear']").linkbutton({text:'删除',plain:true,iconCls:'icon-remove'});
        //     }
        // });

        //文章id遍历选取
        var cateId =  (window.location.search).substr(1);
        initList(cateId);
        function initList(cateId){
            $('#dg').datagrid({
                url:'http://localhost:3000/comments/list',
                method:'post',
                striped:true,   //设置表单渐变色
                queryParams:{newsID:cateId},
                rownumbers:true,
                singleSelect:true,
                pageSize:6,
                pageList:[6,12,18,24],
                pagination:true,
                multiSort:true,
                fitColumns:true,
                // fit:true,
                // onClickCell: onClickCell,
               columns:[[
                { field:'null',checkbox:true},
                { field:'newsTitle',title:'评论文章标题',width:$(this).width()*0.04,sortable:true,editor:'text'},
                { field:'newsAuthor',title:'作者',width:$(this).width()*0.03,sortable:true,editor:'text'},
                { field:'classifyID',title:'类别',width:$(this).width()*0.04,sortable:true},
                { field:'date',title:'日期',width:$(this).width()*0.04,sortable:true},
                { field:'content',title:'评论内容',width:$(this).width()*0.1,sortable:true,editor:'text'},
                { field:'username',title:'评论人',width:$(this).width()*0.04,sortable:true},
                { field:'operate',title:'评论管理',align:'center',width:$(this).width()*0.08,
                    formatter:function(value, row, index){
                        var str = '<a href="javascript:;" name="clear" class="easyui-linkbutton" onClick="commentsClear()" >';
                        return str;
                    }}
                ]],
                onLoadSuccess:function(data){
                   $("a[name='clear']").linkbutton({text:'删除',plain:true,iconCls:'icon-remove'});
                }
            });
        }


        //刷新数据
        function reload(){
            $('#dg').datagrid('reload');
        }
        //返回评论操作页
        function replace(){
            addTab_public('评论操作','file/layout-comments.html','icon-user-group',1);
        }
        //返回评论也进行选择以及添加
        function openAdd(){
            addTab_public('评论操作','file/layout-comments.html','icon-user-group',1);
            alert("请选择文章进行添加评论");
        }
        //删除评论
        function commentsClear(){
            var items = $('#dg').datagrid('getSelections');
            $.messager.confirm('信息提示','确定要删除该记录？', function(result){
                if(result){//result是一个true的布尔值
                    var ids = [];
                    $(items).each(function(){
                        ids.push(this._id);
                    });
                    $.ajax({
                        url:'http://localhost:3000/comments/data/'+ids,
                        type:'DELETE',
                        success:function(data){
                            console.log(data);
                            if(data){
                                $.messager.alert('信息提示','删除成功！','info',()=>{    
                                    reload();
                                });

                            }
                            else{
                                $.messager.alert('信息提示','删除失败！','info');
                            }
                        }
                    })
                }else{

                }
            });
        }
    </script>
    <script type="text/javascript" src="../js/changeTable.js"></script>
</body>
</html>